package cn.taketoday.live.tools;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

import cn.taketoday.live.config.GlobalData;
import cn.taketoday.live.data.room.RoomInit;
import cn.taketoday.live.data.server.HostServer;
import cn.taketoday.live.data.user.UserMedal;
import cn.taketoday.live.http.BilibiliHttpRequests;
import cn.taketoday.live.http.HttpOtherData;
import cn.taketoday.live.http.HttpRoomData;
import lombok.extern.slf4j.Slf4j;

/**
 * @author BanqiJane
 */
@Slf4j
public class CurrencyTools {

  /**
   * 获取随机破站弹幕服务器地址 20201218优化获取
   *
   * @param hostServers
   *
   * @return
   */
  public static String GetWsUrl(List<HostServer> hostServers) {
    StringBuilder stringBuilder = new StringBuilder();
    String wsUrl = null;
    int control = 0;
    if (hostServers.size() > 0) {
      while (!(GlobalData.WebSocketURL).equals(wsUrl)) {
        if (control > 5) {
          break;
        }
        HostServer hostServer = hostServers.get((int) (Math.random() * hostServers.size()));
        stringBuilder.append("wss://");
        stringBuilder.append(hostServer.getHost());
        stringBuilder.append(":");
        stringBuilder.append(hostServer.getWss_port());
        stringBuilder.append("/sub");
        wsUrl = stringBuilder.toString();
        stringBuilder.delete(0, stringBuilder.length());
        control++;
      }
    }
    log.debug("获取破站弹幕服务器websocket地址：{}", wsUrl);
    return wsUrl;
  }

  /**
   * @param time
   *
   * @return
   */
  public static String getGapTime(long time) {
    long hours = time / (1000 * 60 * 60);
    long minutes = (time - hours * (1000 * 60 * 60)) / (1000 * 60);
    long second = (time - hours * (1000 * 60 * 60) - minutes * (1000 * 60)) / 1000;
    String diffTime = "";
    if (minutes < 10) {
      diffTime = hours + ":0" + minutes;
    }
    else {
      diffTime = hours + ":" + minutes;
    }
    if (second < 10) {
      diffTime = diffTime + ":0" + second;
    }
    else {
      diffTime = diffTime + ":" + second;
    }
    return diffTime;
  }
//
//  /**
//   * 获取心跳包byte[]
//   *
//   * @return
//   */
//  public static byte[] heartBytes() {
//    final BarrageHeadHandle barrageHeadHandle =
//            BarrageHeadHandle.getBarrageHeadHandle(
//                    "[object Object]".getBytes().length + 16, PublicDataConf.packageHeadLength,
//                    PublicDataConf.packageVersion, PublicDataConf.heartPackageType, PublicDataConf.packageOther);
//
//    return ByteUtils.byteMerger(WebsocketHandler.BEhandle(barrageHeadHandle), "[object Object]".getBytes());
//  }

  /**
   * 生成uuid 8-4-4-4-12
   *
   * @return
   */
  public static String getUUID() {
    return UUID.randomUUID().toString();
  }

  /**
   * @return 返回MD5
   */
  public static String deviceHash() {
    String hashString = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()+-";
    char[] hashChars = hashString.toCharArray();
    StringBuilder stringBuilder = new StringBuilder(50);
    stringBuilder.append(System.currentTimeMillis()).append(hashChars[(int) (Math.random() * hashChars.length)])
            .append(hashChars[(int) (Math.random() * hashChars.length)])
            .append(hashChars[(int) (Math.random() * hashChars.length)])
            .append(hashChars[(int) (Math.random() * hashChars.length)])
            .append(hashChars[(int) (Math.random() * hashChars.length)]);
    return DigestUtils.md5Hex(stringBuilder.toString());
  }

  /**
   * 过滤房间号
   */
  public static long parseRoomId() {
    if (GlobalData.SHORT_ROOM_ID != null && GlobalData.SHORT_ROOM_ID > 0) {
      return GlobalData.SHORT_ROOM_ID;
    }
    return GlobalData.ROOM_ID;

  }

  /**
   * 获取天气接口用
   */
  public static String getWeatherDay() {
    int week = Calendar.getInstance().get(Calendar.DAY_OF_WEEK) - 1;
    int day = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
    String weekString = "一";
    StringBuilder weatherDay = new StringBuilder();
    switch (week) {
      case 1:
        weekString = "一";
        break;
      case 2:
        weekString = "二";
        break;
      case 3:
        weekString = "三";
        break;
      case 4:
        weekString = "四";
        break;
      case 5:
        weekString = "五";
        break;
      case 6:
        weekString = "六";
        break;
      case 0:
        weekString = "天";
        break;
      default:
        weekString = "一";
        break;
    }
    return weatherDay.append(day).append("日星期").append(weekString).toString();
  }

  public static List<UserMedal> getAllUserMedals() {
    int page = 1;
    List<UserMedal> userMedals = new ArrayList<>();
    List<UserMedal> userMedalsIn = null;
    while (true) {
      userMedalsIn = BilibiliHttpRequests.getMedalList(page);
      if (userMedalsIn == null || userMedalsIn.size() < 1) {
        break;
      }
      userMedals.addAll(userMedalsIn);
      page++;
      try {
        Thread.sleep(500);
      }
      catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
    return userMedals;
  }

  public static String handleEnterStr(String enterStr) {
    String[] enterStrs = null;
    if (StringUtils.indexOf(enterStr, "\n") != -1) {
      enterStrs = StringUtils.split(enterStr, "\n");
    }
    if (enterStrs != null && enterStrs.length > 1) {
      return enterStrs[(int) Math.ceil(Math.random() * enterStrs.length) - 1];
    }
    return enterStr;
  }

  //打卡 保持其同步性
  public synchronized static int clockIn(List<UserMedal> userMedals) {
    Long uid = HttpOtherData.httpGetClockInRecord();
    if (uid != null && uid > 0) return 0;
    if (StringUtils.isEmpty(GlobalData.centerSetConf.getClock_in().getBarrage())) return 0;
    int max = 0;
    RoomInit roomInit;
    if (!CollectionUtils.isEmpty(userMedals)) {
      for (UserMedal userMedal : userMedals) {
        roomInit = HttpRoomData.httpGetRoomInit(userMedal.getRoomid());
        try {
          Thread.sleep(4050);
        }
        catch (InterruptedException e) {
          e.printStackTrace();
        }
        String barrge = handleEnterStr(GlobalData.centerSetConf.getClock_in().getBarrage());
        //   short code = 0;
        short code = BilibiliHttpRequests.sendBarrage(barrge, roomInit.getRoom_id());
        try {
          Thread.sleep(2050);
        }
        catch (InterruptedException e) {
          e.printStackTrace();
        }

        log.debug("第{}次打卡{},直播间:{},up主:{},发送弹幕:{}", max + 1, code == 0 ? "成功" : "失败", userMedal.getRoomid(), userMedal.getTarget_name(),
                  barrge);
        max++;
      }
    }
    return max;
  }

  public static String sendGiftCode() {
    String code = "";
    //默认随机发送
    synchronized(GlobalData.centerSetConf.getThank_gift().getCodeStrings()) {
      if (!CollectionUtils.isEmpty(GlobalData.centerSetConf.getThank_gift().getCodeStrings())) {
        int random = (int) Math.ceil(Math.random() * GlobalData.centerSetConf.getThank_gift().getCodeStrings().size()) - 1;
        int i = 0;
        for (Iterator<String> iterator = GlobalData.centerSetConf.getThank_gift().getCodeStrings().iterator(); iterator.hasNext(); ) {
          if (i == random) {
            code = iterator.next();
            iterator.remove();
            break;
          }
          i++;
        }
      }
    }
    return code;
  }
}
